/**
 * 
 */
package br.unifor.mia.cd.middleware.arcademis.marshalable;

import arcademis.MarshalException;
import arcademis.Stream;
import br.unifor.mia.cd.middleware.util.MarshalableUtil;

/**
 * Serialização de objetos anotados com a anotação {@link Marshalable}
 * 
 * @author landim
 *
 */
public class MarshallerAnnotationMechanism implements MarshallerMechanism {
	
	/**
	 * singleton
	 */
	private static MarshallerAnnotationMechanism instance;
	
	/**
	 * Construtor privado.
	 */
	private MarshallerAnnotationMechanism() {
	}
	
	/**
	 * Pegar instância única
	 * @return singleton object
	 */
	public static MarshallerAnnotationMechanism getInstance() {
		if (instance == null) {
			instance = new MarshallerAnnotationMechanism();
		}
		return instance;
	}

	/**
	 * @see br.unifor.mia.cd.middleware.arcademis.marshalable.MarshallerMechanism#marshall(arcademis.Stream, java.lang.Object)
	 */
	public void marshall(Stream stream, Object bean) throws MarshalException {
		if (bean != null) {
			if (bean.getClass().isAnnotationPresent(Marshalable.class)) {
				Marshalable annotationClazz = bean.getClass().getAnnotation(Marshalable.class);
				Marshaller marshaller = MarshalableUtil.getMarshaller(annotationClazz);
				MarshalableUtil.marshalingFields(stream, bean, marshaller);
				
			} else {
				//Não é serializável.
			}
		}
		
	}

	/**
	 * @see br.unifor.mia.cd.middleware.arcademis.marshalable.MarshallerMechanism#unmarshall(arcademis.Stream, java.lang.Object)
	 */
	public Object unmarshall(Stream stream, Object bean)
			throws MarshalException {
		// TODO Auto-generated method stub
		return null;
	}

}
